package com.zb.zbtpd.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.zb.zbtpd.feignclient.BusinessSchoolFeignClient;
import com.zb.zbtpd.feignclient.NewsFeignClient;
import com.zb.zbtpd.util.JsonUtil;
import com.zb.zbtpd.vo.BusinessSchool;
import com.zb.zbtpd.vo.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/zbt/pd/news")
public class NewsController {

    @Autowired
    private NewsFeignClient newsFeignClient;
    @Autowired
    private BusinessSchoolFeignClient businessSchoolFeignClient;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 查询数据（分页，条件）
     * @param newstypeid
     * @param currentPage
     * @return
     */
    @RequestMapping("/findNews")
    @ResponseBody
    public  Map<String,Object> findNews(String newstypeid,int currentPage){
        String newsJson=newsFeignClient.findNews(newstypeid,currentPage);
        JsonNode newsNode = JsonUtil.jsonNode(newsJson);
        JsonNode newslistJson = newsNode.get("target").get("newslist");
        Iterator<JsonNode> newsEle = newslistJson.elements();
        List<News> newsList=new ArrayList<>();
        while (newsEle.hasNext()){
            JsonNode obj = newsEle.next();
            News n = JSONObject.parseObject(obj.toString(), News.class);
            newsList.add(n);
        }
        Map<String,Object> result=new HashMap<>();
        result.put("totalPage",newsNode.get("target").get("totalPage").toString());
        result.put("count",newsNode.get("target").get("count").toString());
        result.put("newsList",newsList);
        return result;
    }

    /**
     * 创新中心前16条数据
     * @return
     */
    @RequestMapping("/findNewsInnovation")
    @ResponseBody
    public String findNewsInnovation(){
        String newsInnovationTop=stringRedisTemplate.opsForValue().get("newsInnovationTop");
        if(StringUtils.isEmpty(newsInnovationTop)){
            newsInnovationTop=newsFeignClient.findNewsInnovation();
            stringRedisTemplate.opsForValue().set("newsInnovationTop",newsInnovationTop);
        }
        return newsInnovationTop;
    }

    /**
     * 资质认证
     * @return
     */
    @RequestMapping("/findNewsQualification")
    @ResponseBody
    public String findNewsQualification(){
        String newsQualification=stringRedisTemplate.opsForValue().get("newsQualification");
        if(StringUtils.isEmpty(newsQualification)){
            newsQualification=newsFeignClient.findNewsQualification();
            stringRedisTemplate.opsForValue().set("newsQualification",newsQualification);
        }
        return newsQualification;
    }

    /**
     * 科技项目
     * @return
     */
    @RequestMapping("/findNewsTechnolog")
    @ResponseBody
    public String findNewsTechnolog(){
        String newsTechnologList=stringRedisTemplate.opsForValue().get("newsTechnologList");
        if (StringUtils.isEmpty(newsTechnologList)){
            newsTechnologList=newsFeignClient.findNewsTechnolog();
            stringRedisTemplate.opsForValue().set("newsTechnologList",newsTechnologList);
        }
        return newsTechnologList;
    }

    /**
     * 知识产权
     * @return
     */
    @RequestMapping("/findNewsIntellectual")
    @ResponseBody
    public String findNewsIntellectual(){
        String newsIntellectualList=stringRedisTemplate.opsForValue().get("newsIntellectualList");
        if (StringUtils.isEmpty(newsIntellectualList)){
            newsIntellectualList=newsFeignClient.findNewsIntellectual();
            stringRedisTemplate.opsForValue().set("newsIntellectualList",newsIntellectualList);
        }
        return newsIntellectualList;
    }

    /**
     * 根据newsid查询查询news信息并跳转zbtBusinessSchoolDetails.html
     * @param newsid
     * @param model
     * @return
     */
    @RequestMapping("/findNewsById")
    public String findNewsById(String newsid, Model model){
        String jsonNews = newsFeignClient.findNewsById(newsid);
        String jsonBusiness = businessSchoolFeignClient.findABusinessByNewsId(newsid);
        JsonNode node = JsonUtil.jsonNode(jsonNews);
        JsonNode businessNode = JsonUtil.jsonNode(jsonBusiness);
        int code = Integer.parseInt(node.get("code").toString());
        int businessCode = Integer.parseInt(businessNode.get("code").toString());
        String result = "";
        if(HttpStatus.OK.value() == code&&HttpStatus.OK.value() == businessCode){
            News news = JSONObject.parseObject(node.get("target").toString(), News.class);
            BusinessSchool business = JSONObject.parseObject(businessNode.get("target").toString(), BusinessSchool.class);
            model.addAttribute("news",news);
            model.addAttribute("business",business);
            result = "zbtBusinessSchoolDetails";
        }else{
            result = "zbtBusinessSchoolDetails";
        }
        return result;
    }


    /**
     * 根据newsid查询查询news信息并跳转zbtNewsDetails.html
     * @param newsid
     * @param model
     * @return
     */
    @RequestMapping("/findNewsByTypeId")
    public String findNewsByTypeId(String newsid, Model model){
        String jsonNews = newsFeignClient.findNewsById(newsid);
        String jsonBusiness = businessSchoolFeignClient.findABusinessByNewsId(newsid);
        JsonNode node = JsonUtil.jsonNode(jsonNews);
        JsonNode businessNode = JsonUtil.jsonNode(jsonBusiness);
        int code = Integer.parseInt(node.get("code").toString());
        int businessCode = Integer.parseInt(businessNode.get("code").toString());
        String result = "";
        if(HttpStatus.OK.value() == code&&HttpStatus.OK.value() == businessCode){
            News news = JSONObject.parseObject(node.get("target").toString(), News.class);
            BusinessSchool business = JSONObject.parseObject(businessNode.get("target").toString(), BusinessSchool.class);
            model.addAttribute("news",news);
            model.addAttribute("business",business);
            result = "zbtNewsDetails.html";
        }else{
            result = "zbtNewsDetails.html";
        }
        return result;
    }
}
